package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.MajorModel;
import com.wanxi.model.MajorTypeModel;
import com.wanxi.service.MajorTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Li
 * @Date 2023/7/9 10:29
 */
@RestController
@RequestMapping("/back/majorType")
@Api(tags = "服务类型管理",value = "Open Api")
public class MajorTypeController {
    @Autowired
    private MajorTypeService majorTypeService;
    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询服务信息",response = MajorTypeModel.class,httpMethod = "GET")
    public ResultDto findAll(MajorTypeModel majorTypeModel){
        Page<MajorTypeModel> page = new Page<>(majorTypeModel.getPage(),majorTypeModel.getLimit());
        QueryWrapper<MajorTypeModel> queryWrappers = query(majorTypeModel);
        IPage<MajorTypeModel> iPage = majorTypeService.page(page,queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }

    @GetMapping("/findMajorType")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询可用服务信息",response = MajorTypeModel.class,httpMethod = "GET")
    public ResultDto findMajorType(){
        QueryWrapper<MajorTypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable",1);
        List<MajorTypeModel> majorType = majorTypeService.list(queryWrapper);
        return ResultDto.builder()
                .code(0)
                .data(majorType)
                .build();
    }
    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询服务信息",response = MajorTypeModel.class,httpMethod = "GET")
    public ResultDto findById(MajorTypeModel majorTypeModel){
        MajorTypeModel majorType = majorTypeService.getById(majorTypeModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(majorType)
                .build();
    }
    @PostMapping("/addMajorType")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增服务信息",response = MajorTypeModel.class,httpMethod = "POST")
    public ResultDto addMajorType(MajorTypeModel majorTypeModel){
        majorTypeService.save(majorTypeModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }
    @PostMapping("/updateMajorType")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改服务信息",response = MajorTypeModel.class,httpMethod = "POST")
    public ResultDto updateMajorType(MajorTypeModel majorTypeModel){
        majorTypeService.updateById(majorTypeModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }
    @PostMapping("/deleteMajorType")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除服务信息",response = MajorTypeModel.class,httpMethod = "POST")
    public ResultDto deleteMajorType(MajorTypeModel majorTypeModel){
        majorTypeService.removeById(majorTypeModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }
    private QueryWrapper<MajorTypeModel> query(MajorTypeModel majorTypeModel) {
        QueryWrapper<MajorTypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(majorTypeModel.getServiceType() == null), "service_type", majorTypeModel.getServiceType());
        queryWrapper.eq(majorTypeModel.getEnable() != null && majorTypeModel.getEnable()!=-1, "enable", majorTypeModel.getEnable());
        return queryWrapper;
    }
}
